/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package foro.ejb.ejb;

import foro.model.Usuario;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Pedro Fernando
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> implements UsuarioFacadeRemote {
    @PersistenceContext(unitName = "foroEJBModulePU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }
    
    @Override
    public Usuario buscarPorNick(String nick)
    {                
        try
        {
            return (Usuario)em.createNamedQuery("Usuario.findByNick").setParameter("nick", nick).getSingleResult();
        } catch(NoResultException ex)
        {
            return null;
        }
            
    }

    @Override
    public List<Usuario> buscarTodos() {
        try
        {            
            Query q = em.createQuery("SELECT u FROM Usuario u ", Usuario.class);            
            return q.getResultList();
            
        } catch(NoResultException ex)
        {
            return null;
        }
    }        
}
